<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib prefix="sys" uri="/WEB-INF/tld/sys.tld" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>借款管理</title>
    <style type="text/css">
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>

<body class="iframeBody">
<div class="Wrapper">
    <div class="searchArea">
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="fieldBox">
                    <div class="label">借款人</div>
                    <div class="field">
                        <input name="cstMgrNm" id="cstMgrNm" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">借款人工号</div>
                    <div class="field">
                        <input name="cstMgrCd" id="cstMgrCd" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">流程编号</div>
                    <div class="field">
                        <input name="flowNo" id="flowNo" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">借款时间</div>
                    <div class="field">
                        <input ligerui="width: 82" name="startLoanTm" id="startLoanTm" type="text" class="ui-datepicker"
                               value="">
                        -
                        <input ligerui="width: 82" name="endLoanTm" id="endLoanTm" type="text" class="ui-datepicker"
                               value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">借款金额</div>
                    <div class="field">
                        <input ligerui="width: 82" name="minLoanAmt" id="minLoanAmt" type="text" class="ui-textbox"
                               value="" validate="{number:true}">
                        -
                        <input ligerui="width: 82" name="maxLoanAmt" id="maxLoanAmt" type="text" class="ui-textbox"
                               value="" validate="{number:true}">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">所属机构</div>
                    <div class="field">
                        <input name="buOrgCd" id="buOrgCd" placeholder="请选择" ltype="combobox">
                    </div>
                </div>

                 <div class="fieldBox" id="searchPrjTypCdId">
              <div class="label">借款状态</div>
                <div class="field">
                <select name="loanStsCd"  id="loanStsCd" >
                 <option value="" selected="selected">请选择</option>
                 <c:forEach items="${loanStsCdList}" var="opt">
                  <option value="${opt.key}">${opt.value}</option>
                 </c:forEach>
                </select>
                </div>
            </div>
                <div class="formBtns notRight">
                    <span id="searchBtn" class="sq-button sq-button-round">查询</span>
                    <span id="clearBtn" class="sq-button sq-button-round">清空</span>
                    <%--<span id="loanBtn" class="sq-button sq-button-round">借款申请</span>--%>
                    <sys:funcAuth resCd="loanApply">
                        <input name="hiddenId" id="hiddenId" type="hidden" value="1">
                        <span id="loanBtn" class="sq-button sq-button-round">借款申请</span>
                    </sys:funcAuth>
                </div>
                <div class="clearFix"></div>
            </div>
        </form>
    </div>
    <div id="ligergrid1" class="sq-grid"></div>
</div>



<script type="text/javascript">
    var grid = null, form = null,contractGrid = null;
    $(function () {
        $("#loanBtn").click(function(){
            var row = grid.getSelectedRow();
            var url='<%=path%>/capital/finance/loan/apply/toAddPage.do?newFlag=1&isNeedUpload=1';
            if(row){
                url += "?payDtlEditable=1&isMust=1&loanInfoId=" + row.id;
            }
            parent.parent.mainApp.f_addTab('tab_finace_apply','借款申请',url);
        });
        initGrid();
        //事业部
        $("#buOrgCd").ligerComboBox({
            valueField: 'key',
            textField: 'value'
        });
        liger.get("buOrgCd").setData(${buOrgCdList});

        $("#pageloading").hide();

        /* 查询 */
        $("#searchBtn").click(function (event) {
            form = liger.get("searchBar");
            var data = form.getData();
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            grid.reload();
            //detailGrid.reload();
        });

        /* 清空 */
        $("#clearBtn").click(function (event) {
            clearLigerForm("searchBar");
        });
    });

    /* 初始化列表*/
    function initGrid(){
        var url = '<%=path%>/finance/finLoanInfo/initList.do';
        grid = $("#ligergrid1").ligerGrid({
                url:url,
                method: 'get',
                columns: [
                    /*{display: '客户名称', name: 'cstNm', minWidth: 200, align: 'left', enabledSort: false, render: function (rowdata, rowindex, value, column) {
                            if(StringUtils.isEmpty(value)){
                                value = "";
                            }
                            var h = '';
                            h='<a href="javascript:showDetail(';
                            h+="'"+rowdata.cstId+"'";
                            h+=')">'+value+'</a>'
                            return h;
                        }},*/
                    {display: '借款人', name: 'cstMgrNm', width: 150, align: 'right', enabledSort: false},
                    {display: '借款人工号', name: 'cstMgrCd', width: 150, align: 'right', enabledSort: false},
                    {display: '借款时间', name: 'loanTm', width: 100, align: 'right', enabledSort: false,render: renderDate},
                    {display: '所属机构', name: 'buOrgCd', width: 150, align: 'right', enabledSort: false,
                        render: function (rowdata, index, value) {
                            var buCd = '';
                            if(rowdata.buOrgCd!='0000'){
                                buCd = rowdata.buOrgCd;
                            }
                            return buCd;
                        }
                    },
                    {
                        display: '借款总金额(元)', name: 'loanAmt', width: 100, align: 'right', enabledSort: false,
                        render: function (rowdata, index, value) {
                            return '&yen;' + FormatMoney(rowdata.loanAmt/100);
                        }
                    },
                    {
                        display: '实付总金额(元)', name: 'actPayAmtSum', width: 150, align: 'right', enabledSort: false,
                        render: function (rowdata, index, value) {
                            return '&yen;' + FormatMoney(rowdata.actPayAmtSum / 100);
                        }
                    },
                    {display: '借款状态', name: 'loanStsCd', width: 100, align: 'right', enabledSort: false},
                    {
                        display: '待付款金额(元)', name: 'pendPayAmt', width: 130, align: 'right', enabledSort: false,
                        render: function (rowdata, index, value) {
                            return '&yen;' + FormatMoney(rowdata.pendPayAmt / 100);
                        }
                    },
                ],
                pageSize: 10,
                root: 'rows',
                record: 'total',
                hideLoadButton: true,
                width: '100%',
                height: '100%',
                heightDiff: -2,
                allowUnSelectRow: true,
                alternatingRow: false,
                rownumbers: false,  //显示序号
                onBeforeShowData: function () {
                    var grid = this;
                    grid.collapsedRows = [];
                },
                detail: {
                    height: 'auto',
                    onShowDetail: showContractGrid
                }
            })
        grid.setHeight(100);
        form = $("#searchBar").ligerForm();
    }

    function getSearchParam(){
        form = liger.get("searchBar");
        var data = form.getData();
        return data;
    }

    /**
     * 显示客户下的逾期合同
     */
    function showContractGrid(row,detailPanel,callback){

        var gridBox = document.createElement('div');
        $(gridBox).css('margin', 10);
        $(detailPanel).css({
            'padding': 10,
            'box-sizing': 'border-box'
        }).append(gridBox);
        var searchParam = getSearchParam();
        contractGrid = $(gridBox).ligerGrid({
            url: '<%=path%>/finance/finPayApplyInfo/selectList.do?loanId=' + row.id,
            parms:searchParam,
            method: 'get',
            columns: [
                {
                    display: '借款单号', name: 'loanNo', width: 120
                },
                { display: '实付时间', name: 'actPayTm', minWidth: 100 , enabledSort:true,render:renderDate},
                {
                    display: '实际付款金额(元)', name: 'actPayAmtSum', width: 120, align: 'right', enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '&yen;' + FormatMoney(rowdata.actPayAmtSum / 100);
                    }
                },
                { display: '放款状态', name: 'payStsCdNm', width: 130, enabledSort:false},
                { display: '付款单号', name: 'pNo',  width: 180, enabledSort:false,
                    render: function (rowdata, rowindex, value, column) {
                        if(StringUtils.isEmpty(value)){
                            value = "";
                        }
                        var h = '';
                        h='<a href="javascript:showFlow(';
                        h+="'"+rowdata.pInstId+"',";
                        h+="'"+rowdata.audStsCd+"',";
                        h+="'"+rowdata.dataId+"',";
                        h+="'"+rowdata.formUrl+"'";
                        h+=')">'+value+'</a>'
                        return h;
                    }
                },
                {
                    display: '操作', width: 180, align: 'left', enabledSort: false,
                    render: function (rowdata, index, value) {
                        var res = "";
                        //var editBtn = getEditBtn(rowdata.id,rowdata.pNo);
                        var editBtn = getEditBtn(rowdata.id,rowdata.payStsCd);
                        res += editBtn;
                        if(rowdata.payStsCd == '0'){
                            if($("#hiddenId").val()) {
                                res += '<a class="sq-button-hasicon" style="cursor: pointer;" onclick="startFlow(' + rowdata.id + ');"><i class="sqBtnIco_50"></i>发起申请</a> ';
                            }
                        }else{
                        }
                        return res;
                    }
                },
            ],
            root: 'rows',
            hideLoadButton: true,
            width: '100%',
            height: 'auto',
            isScroll: false,
            rownumbers: true,  //显示序号
            checkbox: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            onAfterShowData: callback,
            frozen: false,
            usePager: false
        });
    }
    function getEditBtn(id,payStsCd){
        if(payStsCd=='0'){
            return '<a class="sq-button-hasicon" style="cursor: pointer;" onclick="editApply(' + id + ',\'' + payStsCd + '\');"><i class="sqBtnIco_49"></i>修改</a>  ';
        }else{
            return '<a class="sq-button-hasicon" style="cursor: pointer;" onclick="editApply(' + id + ',\'' + payStsCd + '\');"><i class="sqBtnIco_49"></i>查看</a>  ';
        }
    }
    function startFlow(payApplyId){
        var url = "<%=path%>/capital/finance/loan/apply/startFlow.do";
        $.get(url,{payApplyId:payApplyId},function(res){
            res = JSON.parse(res);
            alert(res.info);
        })
    }
    /**
     * 修改借款申请
     * @param payApplyId
     */
    function editApply(payApplyId,payStsCd){
        var url = '<%=path%>/capital/finance/loan/apply/toAddPage.do?isNeedUpload=1&applyInfoId='+payApplyId;
        if(payStsCd=='0'){ //已经发起流程的就不允许修改
            parent.Add_navTab('tab_fiance_apply_edit_'+payApplyId,'修改申请',url);
        }else{
            url += "&isMust=1&isAccount=1&showSaveApplyBtn=0&payDtlEditable=0"
            parent.Add_navTab('tab_fiance_apply_edit_'+payApplyId,'修改申请',url);
        }
    }
    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }
    //到流程跟踪页
    function showFlow(pInstId,audStsCd,dataId,formUrl){
        if(StringUtils.isEmpty(pInstId)){
            UIUtils.alertWarning(CommonConstant.C_P_INSTANCEID_NOT_HERE);
            return;
        }
        var params = "?pInsId="+pInstId+"&status="+audStsCd+"&businessKey="+dataId+"&formUrl="+formUrl;
        Add_navTab('finloaninfo'+pInstId,'查看流程',"<%=path%>/flow/flow/toFlowShowPageFowBus.do"+params);
    }
</script>

</body>
</html>
